<?php
namespace Admin\Model;
use Think\Model;
class ActivityModel extends Model {

      protected $_validate = array(
      array('aname','require','活动名称不得为空！',1), 
      array('aname','','活动名称不得重复！',1,unique,3), 
      array('totalticket','require','票数不得为空！',1), 
      array('totalticket','number','请填写合理的票数'),
    );


     public function _before_insert(&$data,$option){
        if($_FILES['thumb']['tmp_name']){
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
            // $upload->exts      =     array( 'txt', 'pdf','docx','ppt','doc');
            // $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg','doc');// 设置附件上传类型
            // $upload->autoSub = false;
            // $upload->saveName ='';
            $upload->rootPath  =   './Public/UPLOADS/activity/';// 设置附件上传目录
            $upload->savePath  =   ''; 
            $info   =   $upload->uploadOne($_FILES['thumb']);
            // dump($info);die;
            $thumb=$info['savepath'].$info['savename']; 
            $data['thumb']=$thumb;
            $data['attach']=$info['name'];
        }
        $data['restticket']=$data['totalticket'];
        $data['adate']=date ( "Y-m-d H:i:s" ); 
    }

     public function _before_update(&$data,$option){
         
        $enddate=I('_enddate');
        $startdate=I('_startdate');
        $bmdate=I('_bmdate');
        if (!$data['startdate']) {
           $data['startdate']=$startdate;
        }
        if (!$data['enddate']) {
           $data['enddate']=$enddate;
        }
        if (!$data['bmdate']) {
           $data['bmdate']=$bmdate;
        }
        if($_FILES['thumb']['tmp_name']){
            if(I('oldpic')){
               $url='./Public/UPLOADS/activity/' . I('oldpic');
               unlink($url);
            }
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     3145728 ;// 设置附件上传大小
              // $upload->exts      =     array( 'txt', 'pdf','docx','ppt','doc','zip');
            $upload->rootPath  =   './Public/UPLOADS/activity/';// 设置附件上传目录
            $upload->savePath  =   ''; 
             // $upload->saveName ='';
            $info   =$upload->uploadOne($_FILES['thumb']);
            // dump($info);die;
            $thumb=$info['savepath'].$info['savename']; 
            $data['attach']=$info['name'];
            $data['thumb']=$thumb;;
        }
    }
    public function _before_delete($option){
        $id=$option['where']['aid'];
        $res=$this->field('thumb')->find($id);
        if($res['thumb']){
            $url='./Public/UPLOADS/activity/' . $res['thumb'];   // 把目录名存到file中
            unlink ( $url );  
        }

    }

}